package com.example.footballsystem.service;

import com.example.footballsystem.entity.Game;
import com.example.footballsystem.entity.SeriesGame;
import com.example.footballsystem.entity.User;
import com.example.footballsystem.mapper.GameMapper;
import com.example.footballsystem.mapper.GameUserMapper;
import com.example.footballsystem.mapper.SeriesGameMapper;
import com.example.footballsystem.mapper.UserAccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SeriesGameService {
    @Autowired
    SeriesGameMapper seriesGameMapper;
    @Autowired
    GameMapper gameMapper;
    @Autowired
    GameUserMapper gameUserMapper;
    @Autowired
    UserAccountMapper userAccountMapper;
    public List<SeriesGame> getAllSeriesGame(){
        Timestamp now = new Timestamp(System.currentTimeMillis());

        for(SeriesGame seriesGame : seriesGameMapper.getAllSeriesGame()){
            Timestamp startTime = seriesGame.getSelectedDate();
            if(now.getTime()-startTime.getTime()>+3*60*60*1000){    //结束
                seriesGameMapper.updateState(seriesGame.getSeriesID(),"已结束");
            }else if(0<now.getTime()-startTime.getTime()&&now.getTime()-startTime.getTime()<3*60*60*1000){
                seriesGameMapper.updateState(seriesGame.getSeriesID(),"进行中");
            }else if(now.getTime()-startTime.getTime()<0&&seriesGame.getPersonNum()<2*seriesGame.getCompetitionSystem()){
                seriesGameMapper.updateState(seriesGame.getSeriesID(),"报名中");
            }else{
                seriesGameMapper.updateState(seriesGame.getSeriesID(),"已满");
            }
        }

        return seriesGameMapper.getAllSeriesGame();
    }
    public List<Game> getAllGame(){
        return gameMapper.getAllGame();
    }
    public List<Game> getGameBySeriesID(int SeriesID){
        return gameMapper.getGameBySeriseID(SeriesID);
    }
    public User getHost(int seriesID){
        //log.info("host           12   "+seriesGameMapper.getHostAccount(seriesID));
        return userAccountMapper.getUserByAccount(seriesGameMapper.getHostAccount(seriesID));
    }
    public List<User> getUserInGame(int seriesID){
        List<String> accountList =  gameUserMapper.getUserInGame(seriesID);
        List<User> uesrList = new ArrayList<>();
        for(int i=0;i< accountList.size();i++){
            log.info("00000000000000"+accountList.toString());
            uesrList.add(userAccountMapper.getUserByAccount(accountList.get(i)));
        }

        return uesrList;
    }

    public Boolean createSeriesGame(SeriesGame seriesGame){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        seriesGame.setCreateDate(time.toString());
        return seriesGameMapper.addSeriesGame(seriesGame);
    }
    //移动端添加新系列赛
    public Boolean addSeriesGame(SeriesGame seriesGame){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        seriesGame.setCreateDate(time.toString());
        seriesGame.setSeriesID(seriesGameMapper.getMaxID()+1);
        switch (seriesGame.getCompetitionSystem()){
            case 5:
                log.info("555555555555");
                seriesGame.setLogPath("https://bdimg6.qunliao.info/fastdfs7/M00/90/0E/280x210/crop/-/rBUBsmaM8y2AWbBSAADfBKG-HNY516.jpg");
                break;
            case 7:
                log.info("777777777777");
                seriesGame.setLogPath("https://ts1.cn.mm.bing.net/th/id/R-C.5f867dd99db7c4ecad16f0ddc9efa446?rik=WBu81B6swGmLIg&riu=http%3a%2f%2fpic.ntimg.cn%2ffile%2f20171210%2f6355359_190602218032_2.jpg&ehk=s9r27RCOWDH11PydeijABc6szyBdaiY0gzrYbE5KXkA%3d&risl=&pid=ImgRaw&r=0HNY516.jpg");
                break;
            case 11:
                log.info("111111111111");
                seriesGame.setLogPath("https://img.zcool.cn/community/01463a5ab9bd9ea8012062e342c006.jpg@2o.jpg");
                break;
        }
        log.info("ser "+seriesGame.toString());
        return seriesGameMapper.addSeriesGame(seriesGame);
    }
    public Boolean addGame(Game game){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        game.setCreateDate(time.toString());
        return gameMapper.addGame(game);
    }

    public Boolean deleteSeriesGame(int SeriesID){
        return seriesGameMapper.deleteSeriesGame(SeriesID);
    }
    public Boolean deleteGame(int SeriesID,int gameID){
        return gameMapper.deleteGame(SeriesID,gameID);
    }

    public Boolean joinGame(int seriesID,String account){
            List<Integer> mineGame = gameUserMapper.getMineGame(account);
            String state = seriesGameMapper.getSeriesGame(seriesID).getState();
            if (Objects.equals(state, "报名中")){
                for(int i=0;i<mineGame.size();i++){
                    if(mineGame.get(i)==seriesID){
                        log.info("已经参加过啦");
                        return false;
                    }
                }
                SeriesGame seriesGame1;
                seriesGame1 = seriesGameMapper.getSeriesGame(seriesID);
                //log.info(seriesGame1.getCompetitionSystem());
                int rule=seriesGame1.getCompetitionSystem();//赛制
                if(seriesGame1.getPersonNum()<=2*rule){
                    seriesGameMapper.addPersonNum(seriesID);          //比赛人数加一
                    Timestamp time = new Timestamp(System.currentTimeMillis());
                    gameUserMapper.addPlayerGame(seriesID,account,time.toString());             //更新game_user表
                    return true;
                }else {
                    return false;
                }
            }else{
                return false;
            }

    }
    //获取参加的比赛
    public List<SeriesGame> getJoinGame(String account){
        List<Integer> seriesIDList = new ArrayList<>();
        seriesIDList = gameUserMapper.getMineGame(account);
        List<SeriesGame> seriesGames = new ArrayList<>();
        for(int i=0;i<seriesIDList.size();i++){
            seriesGames.add(seriesGameMapper.getSeriesGame(seriesIDList.get(i)));
        }
        return seriesGames;
    }
    //获取创建的比赛
    public List<SeriesGame> getMasterGame(String account){
        return seriesGameMapper.getSeriesGameByAccount(account);
    }
    //获取参与比赛数
    public List<Integer> getSeriesCount(){
        return gameUserMapper.getUserCount();
    }
    //获取参加比赛总数
    public Integer count(){
        return gameUserMapper.count();
    }
}
